import pandas as pd
#import plotly_express as px
import plotly.express as px
d = {'country': ["Argentina", "Argentina", "Argentina", "Argentina", "Argentina", "Argentina", "Germany", "Germany", "Germany", "Germany", "Germany", "Germany"],
'year': [2005, 2006, 2007, 2008, 2009, 2010, 2005, 2006, 2007, 2008, 2009, 2010],
'education': [3.860, 4.128, 4.462, 4.844, 5.531, 5.019, 4.134, 4.279, 4.343, 4.41, 4.88, 4.914],
'trade': [34.739, 34.700, 34.947, 35.258, 28.367, 29.502, 64.143, 67.105, 69.076, 70.122, 59.871, 67.711]
}
dataset = pd.DataFrame(data = d)
dataset.head()
# Take only the rows with Argentina
#datasetArgentina = dataset.query("country == 'Argentina'")
#px.line(datasetArgentina, x="year", y="trade", line_group="country")
fig = px.line(dataset, x="year", y="trade", line_group="country", color="country", template="plotly_white",
color_discrete_map = dict(Argentina = "#4daf4a", Germany = "#377eb8"),
labels = dict(year = "Year", trade = "Merchandise Trade (% of GDP)"),
range_y = [0, 100],
render_mode = 'svg'
)
fig.update(layout = dict(
legend = dict(orientation = 'h', y = 1.1, x = 0.5)
))
# Trade on scatterplot
fig = px.scatter(dataset, x="year", y="trade", color="country", template="plotly_white",
color_discrete_map = dict(Argentina = "#4daf4a", Germany = "#377eb8"),
labels = dict(year = "Year", trade = "Merchandise Trade (% of GDP)"),
range_y = [0, 100],
render_mode = 'svg'
)
fig.update(layout = dict(
legend = dict(orientation = 'h', y = 1.1, x = 0.5)
))
# Trade on world map
# Create an animated choropleth map
px.choropleth(dataset, locationmode="country names", locations="country",
color = "trade", hover_name = "country",
animation_frame = "year", color_continuous_scale = px.colors.sequential.Plasma, projection = 'robinson')
# Animation with Mapbox choropleth
#px.choropleth_mapbox(dataset, locationmode="country names", locations="country",
# color = "trade", hover_name = "country",
# animation_frame = "year", color_continuous_scale = px.colors.sequential.Plasma, projection = 'robinson')
# Example from https://plot.ly/python/mapbox-county-choropleth/
from urllib.request import urlopen
import json
with urlopen('https://raw.githubusercontent.com/plotly/datasets/master/geojson-counties-fips.json') as response:
counties = json.load(response)
import pandas as pd
#df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/fips-unemp-16.csv",
df = pd.read_csv("fips-unemp-16-modified.csv",
dtype={"fips": str})
#df.head()
fig = px.choropleth_mapbox(df, geojson=counties, locations='fips', color='unemp',
color_continuous_scale="Viridis",
range_color=(0, 12),
mapbox_style="carto-positron",
zoom=3, center = {"lat": 37.0902, "lon": -95.7129},
opacity=0.5,
labels={'unemp':'unemployment rate'},
animation_frame="year"
)
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
fig.show()